package com.net.xpay.backend.scheduler;


import com.google.common.collect.Lists;
import com.net.xpay.common.domain.User;
import com.net.xpay.common.manager.UserManager;
import com.net.xpay.core.domain.PayTransBatch;
import com.net.xpay.core.enums.PayTransBatchStatus;
import com.net.xpay.core.manager.PayTransBatchManager;
import com.net.xpay.core.model.PayTransBatchSearchModel;
import com.net.xpay.core.service.PayTransBatchService;
import com.net.shard.ShardDataSource;
import com.net.shard.ShardDataSourceConfigurer;
import com.net.shard.ShardUtil;
import com.net.shard.domain.ShardConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 2019/7/15 16:28

 */
@Slf4j
@Component
public class PayTransCleanScheduler {

    @Autowired
    @Qualifier("shardDataSource")
    private DataSource shardDataSource;

    @Autowired
    private PayTransBatchService payTransBatchService;

    @Autowired
    private PayTransBatchManager payTransBatchManager;

    @Autowired
    private UserManager userManager;

    /**
     * 关闭已导入和未付款的
     */
    @Scheduled(cron = "0 0 */1 * * *")
    public void closePayTransBatch() {
        ShardDataSourceConfigurer shardDataSourceConfigurer = ((ShardDataSource) shardDataSource).getShardDataSourceConfigurer();
        Map<Long, ShardConfig> shardConfigMap = shardDataSourceConfigurer.getShardConfigMap();
        for (Long shardId : shardConfigMap.keySet()) {
            try {
                ShardUtil.setShardContext(shardId);
                doClosePayTransBatch();
            } catch (Exception e) {
                log.warn("closePayTransBatch error", e);
            } finally {
                ShardUtil.cleanShardContext();
            }
        }
    }

    private void doClosePayTransBatch() {
        PayTransBatchSearchModel searchModel = new PayTransBatchSearchModel();
        searchModel.setStatusList(Lists.newArrayList(PayTransBatchStatus.IMPORTED, PayTransBatchStatus.WAIT_PAY));
        LocalDateTime maxCreateTime = LocalDateTime.now().minusDays(3L);
        searchModel.setMaxCreateTime(maxCreateTime);
        List<PayTransBatch> batches = payTransBatchManager.list(searchModel);
        log.info("doClosePayTransBatch size={}", batches.size());
        for (PayTransBatch batch : batches) {
            try {
                User user = userManager.getById(batch.getUserId());
                payTransBatchService.closePayTransBatch(user, batch.getId());
                log.info("doClosePayTransBatch success|importBatchNo={}", batch.getImportBatchNo());
            } catch (Exception e) {
                log.info("doClosePayTransBatch failed|importBatchNo={}", batch.getImportBatchNo(), e);
            }
        }
        log.info("doClosePayTransBatch finished");
    }


}
